﻿// -----------------------------------------------------------------------
// <copyright file="LanguageRepository.cs" company="AGR">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace AGR.Data.Repository.Contract
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using AGR.Model;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class LanguageRepository : AbstractRepository<Language>
    {
        public LanguageRepository()
        {
            base._contextHelper = null;
            base._context = new Context.AGRContext();
        }

        public override IQueryable<Language> GetAll()
        {
            var result = (from x in base._context.Languages
                          select new Language
                          {
                              Id = x.Id,
                              LanguageName = x.LanguageName,
                              Displayed = x.Displayed,
                              DateCreated = x.DateCreated,
                              UserCreated = x.UserCreated,
                              DateModified = x.DateModified,
                              UserModified = x.UserModified
                          });

            return result;
        }

        public override void Add(Language entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Language entityToAdd = new Context.Language
            {
                LanguageName = entity.LanguageName,
                Displayed = entity.Displayed,
                DateCreated = DateTime.Now,
                UserCreated = entity.UserCreated,
                DateModified = null,
                UserModified = null
            };

            base._context.Languages.InsertOnSubmit(entityToAdd);
        }

        public override void Update(Language entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Language entityToUpdate = base._context.Languages.Single(x => x.LanguageName.Equals(entity.LanguageName));

            entityToUpdate.LanguageName = entity.LanguageName;
            entityToUpdate.Displayed = entity.Displayed;
            entityToUpdate.DateModified = DateTime.Now;
            entityToUpdate.UserModified = entity.UserModified;
        }

        public override void Delete(Language entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Language entityToDelete = base._context.Languages.Single(x => x.LanguageName.Equals(entity.LanguageName));

            base._context.Languages.DeleteOnSubmit(entityToDelete);
        }
    }
}
